package com.zhong.gang.applib.rx;

import androidx.collection.ArrayMap;

import java.util.Iterator;
import java.util.Map;

import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.CompositeDisposable;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;

/**
 * Created by ajin on 2018/3/19.
 */

public class RxManager {
    public RxBus mRxBus;
    private ArrayMap<String, Observable<?>> R;
    private CompositeDisposable S;

    public RxManager() {
    }

    public <T> void add(String var1, Consumer<T> var2) {
        if(this.R == null || this.mRxBus == null) {
            this.R = new ArrayMap();
            this.mRxBus = RxBus.get();
            this.S = new CompositeDisposable();
        }
        Observable var3 = this.mRxBus.register(var1);
        this.R.put(var1, var3);
        this.S.add(var3.observeOn(AndroidSchedulers.mainThread()).subscribe(var2));
    }

    public <T> void add(Disposable var1) {
        if(this.S == null) {
            this.S = new CompositeDisposable();
        }

        this.S.add(var1);
    }

    public void clear() {
        if(this.S != null) {
            this.S.clear();
        }

        if(this.R != null) {
            Iterator var1 = this.R.entrySet().iterator();

            while(var1.hasNext()) {
                Map.Entry var2 = (Map.Entry)var1.next();
                this.mRxBus.unregister(var2.getKey(), (Observable)var2.getValue());
            }

            this.R.clear();
        }

        this.R = null;
        this.mRxBus = null;
        this.S = null;
    }

    public void post(Object var1, Object var2) {
        if(this.mRxBus == null) {
            this.mRxBus = RxBus.get();
        }

        this.mRxBus.post(var1, var2);
    }

    public void post(Object var1) {
        if(this.mRxBus == null) {
            this.mRxBus = RxBus.get();
        }

        this.mRxBus.post(var1);
    }
}
